package cn.db101.api.complex.component

import cn.hutool.core.codec.Base64
import cn.hutool.core.lang.UUID
import io.minio.MinioClient
import io.minio.PutObjectArgs
import okhttp3.OkHttpClient
import okhttp3.Request
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import java.io.ByteArrayInputStream
import java.io.InputStream

/**
 *
 * @author：zooooooooy
 * @date: 2022/7/19 - 9:53
 */
@Component
class MinioComponent {

    @Autowired
    lateinit var minioClient: MinioClient

    @Autowired
    lateinit var minioConfigProperties: MinioConfigProperties

    val faceBuketName: String = "face"

    val okHttpClient = OkHttpClient()

    fun uploadImage(bytes: ByteArray, path: String, fileName: String = UUID.randomUUID().toString(true)): String {

        minioClient.putObject(PutObjectArgs.builder()
            .bucket(faceBuketName)
            .stream(ByteArrayInputStream(bytes), bytes.size.toLong(), -1)
            .`object`("/$path/$fileName.jpg")
            .contentType("image/jpeg")
            .build())

        return minioConfigProperties.outEndpoint + "/$faceBuketName/$path/$fileName.jpg"

    }

    fun uploadImageByUrl(url: String, path: String, fileName: String = UUID.randomUUID().toString(true)): String {

        val bytes = okHttpClient.newCall(
            Request.Builder()
                .get()
                .url(url).build()
        ).execute().body?.bytes()

        return uploadImageByUrl(bytes!!, path, fileName)

    }

    fun uploadImageByUrl(bytes: ByteArray, path: String, fileName: String = UUID.randomUUID().toString(true)): String {

        minioClient.putObject(bytes?.size?.toLong()?.let {
            PutObjectArgs.builder()
                .bucket(faceBuketName)
                .stream(ByteArrayInputStream(bytes), it, -1)
                .`object`("/$path/$fileName.jpg")
                .contentType("image/jpeg")
                .build()
        })

        return minioConfigProperties.outEndpoint + "/${faceBuketName}/$path/$fileName.jpg"
    }

    fun uploadFileByUrl(url: String, buketName: String, path: String, fileName: String): String {

        val bytes = okHttpClient.newCall(
            Request.Builder()
                .get()
                .url(url).build()
        ).execute().body?.bytes()

        minioClient.putObject(bytes?.size?.toLong()?.let {
            PutObjectArgs.builder()
                .bucket(buketName)
                .stream(ByteArrayInputStream(bytes), it, -1)
                .`object`("/$path/$fileName")
                //.contentType("image/jpeg")
                .build()
        })

        return minioConfigProperties.outEndpoint + "/$buketName/$path/$fileName"

    }

    fun uploadFileByUrl(bytes: ByteArray, buketName: String, path: String, fileName: String): String {

        minioClient.putObject(bytes?.size?.toLong()?.let {
            PutObjectArgs.builder()
                .bucket(buketName)
                .stream(ByteArrayInputStream(bytes), it, -1)
                .`object`("/$path/$fileName")
                .build()
        })

        return minioConfigProperties.outEndpoint + "/$buketName/$path/$fileName"

    }

    fun imageToBase64(url: String): String {
        val bytes = okHttpClient.newCall(
            Request.Builder()
                .get()
                .url(url).build()
        ).execute().body?.bytes()

        return Base64.encode(bytes)

    }

    fun urlFileToInputStream(url: String): InputStream? {

        return okHttpClient.newCall(
            Request.Builder()
                .get()
                .url(url).build()
        ).execute().body?.byteStream()
    }

}
